﻿using Microsoft.EntityFrameworkCore;
using S.BreedingHistory.Domain.BreedingHistoryDto;
using S.BreedingHistory.Domain.Model;
using S.BreedingHistory.Domain.MyDbContexts;
using S.BreedingHistory.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.BreedingHistory.Infrastructure.Imp
{
    public class BreedingHistoryRepository : BaseRepository<BreedingHistoryModel>, IBreedingHistoryRepository
    {
        private readonly MyDbContext db;

        public BreedingHistoryRepository(MyDbContext db) 
            : base(db)
        {
            this.db = db;
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="breedingHistoryList"></param>
        /// <returns></returns>
        public async Task<bool> BatchAddAsync(List<BreedingHistoryModel> breedingHistoryList)
        {
           
            await db.BreedingHistoryModel.AddRangeAsync(breedingHistoryList);
            var result = await db.SaveChangesAsync();
            return result > 0;
        }
        /// <summary>
        /// 批量修改种母历史记录
        /// </summary>
        /// <param name="breedingHistoryModels"></param>
        /// <returns></returns>
        public async Task<bool> BatchUpdateBreedHistory(List<BreedingHistoryModel> breedingHistoryModels)
        {
            db.BreedingHistoryModel.UpdateRange(breedingHistoryModels);
            var aa = await db.SaveChangesAsync();
           return aa>0;
        }
        /// <summary>
        /// 通过羊只耳号获取最新的种母历史记录
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        public Task<BreedingHistoryModel> GetNewBreedingHistoryByEarTag(string? EarTag)
        {
            return db.BreedingHistoryModel.OrderByDescending(m => m.CreateDate).FirstOrDefaultAsync(m=>m.EarTag == EarTag);
        }

        /// <summary>
        /// 种母历史记录反填
        /// </summary>
        /// <param name="BHId"></param>
        /// <returns></returns>
        public async Task<BreedingHistoryModel> GetBreedingHistoryById(string EITag)
        {
            return await db.BreedingHistoryModel.FirstOrDefaultAsync(m=>m.EarTag== EITag&&m.IsEditor==true);
        }
        /// <summary>
        /// 通过主键反填种母历史记录
        /// </summary>
        /// <param name="bhrId"></param>
        /// <returns></returns>
        public async Task<BreedingHistoryModel> GetBreedingHistoryModelById(int bhrId)
        {
           return await  db.BreedingHistoryModel.FirstOrDefaultAsync(m=>m.BDHRId==bhrId);
        }

        /// <summary>
        /// 通过种母Id查询档案卡
        /// </summary>
        /// <param name="ELTag"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<BreedingHistoryModel>> GetFileCardById(string ELTag)
        {
            return await db.BreedingHistoryModel.Where(m => m.EarTag == ELTag).ToListAsync();
        }
    }
}
